var util = require('../../utils/util.js');

var api = require('../../config/api.js');

Page({
  data: {
    navList: [],
    categoryList: [],
    currentCategory: {},
    goodsCount: 0,
    nowIndex: 0,
    nowId: 0,
    list: [],
    allPage: 1,
    allCount: 0,
    size: 8,
    hasInfo: 0,
    showNoMore: 0,
    loading: 0,
    index_banner_img: 0
  },
  onLoad: function (options) {},
  getChannelShowInfo: function (e) {
    let that = this;
    util.request(api.ShowSettings).then(function (res) {
      if (res.errno === 0) {
        let index_banner_img = res.data.index_banner_img;
        that.setData({
          index_banner_img: index_banner_img
        });
      }
    });
  },
  onPullDownRefresh: function () {
    tt.showNavigationBarLoading();
    this.getCatalog();
    tt.hideNavigationBarLoading(); //完成停止加载

    tt.stopPullDownRefresh(); //停止下拉刷新
  },
  getCatalog: function () {
    //CatalogList
    let that = this;
    util.request(api.CatalogList).then(function (res) {
      that.setData({
        navList: res.data.categoryList
      });
    });
    util.request(api.GoodsCount).then(function (res) {
      that.setData({
        goodsCount: res.data.goodsCount
      });
    });
  },
  getCurrentCategory: function (id) {
    let that = this;
    util.request(api.CatalogCurrent, {
      id: id
    }).then(function (res) {
      that.setData({
        currentCategory: res.data
      });
    });
  },
  getCurrentList: function (id) {
    let that = this;
    util.request(api.GetCurrentList, {
      size: that.data.size,
      page: that.data.allPage,
      id: id
    }, 'POST').then(function (res) {
      if (res.errno === 0) {
        let count = res.data.count;
        that.setData({
          allCount: count,
          allPage: res.data.currentPage,
          list: that.data.list.concat(res.data.data),
          showNoMore: 1,
          loading: 0
        });

        if (count == 0) {
          that.setData({
            hasInfo: 0,
            showNoMore: 0
          });
        }
      }
    });
  },
  onShow: function () {
    this.getChannelShowInfo();
    let id = this.data.nowId;
    let nowId = tt.getStorageSync('categoryId');

    if (id == 0 && nowId === 0) {
      return false;
    } else if (nowId == 0 && nowId === '') {
      this.setData({
        list: [],
        allPage: 1,
        allCount: 0,
        size: 8,
        loading: 1
      });
      this.getCurrentList(0);
      this.setData({
        nowId: 0,
        currentCategory: {}
      });
      tt.setStorageSync('categoryId', 0);
    } else if (id != nowId) {
      this.setData({
        list: [],
        allPage: 1,
        allCount: 0,
        size: 8,
        loading: 1
      });
      this.getCurrentList(nowId);
      this.getCurrentCategory(nowId);
      this.setData({
        nowId: nowId
      });
      tt.setStorageSync('categoryId', nowId);
    }

    this.getCatalog();
  },
  switchCate: function (e) {
    let id = e.currentTarget.dataset.id;
    let nowId = this.data.nowId;

    if (id == nowId) {
      return false;
    } else {
      this.setData({
        list: [],
        allPage: 1,
        allCount: 0,
        size: 8,
        loading: 1
      });

      if (id == 0) {
        this.getCurrentList(0);
        this.setData({
          currentCategory: {}
        });
      } else {
        tt.setStorageSync('categoryId', id);
        this.getCurrentList(id);
        this.getCurrentCategory(id);
      }

      tt.setStorageSync('categoryId', id);
      this.setData({
        nowId: id
      });
    }
  },
  onBottom: function () {
    let that = this;

    if (that.data.allCount / that.data.size < that.data.allPage) {
      that.setData({
        showNoMore: 0
      });
      return false;
    }

    that.setData({
      allPage: that.data.allPage + 1
    });
    let nowId = that.data.nowId;

    if (nowId == 0 || nowId == undefined) {
      that.getCurrentList(0);
    } else {
      that.getCurrentList(nowId);
    }
  }
});